﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Net;
using System.Text;
using System.Windows;
using System.Diagnostics;
using System.IO;
using System.Windows.Controls;

namespace Galaktika.BI.Silverlight.ClientApp.Services
{
    using Galaktika.BI.Runtime.Services;
    using System.Windows.Controls;

    internal class OutputService : IOutputService
    {
        private IServiceProvider m_Services;

        public OutputService(IServiceProvider srv)
        {
           m_Services = srv;
         }

        private T GetService<T>()
           where T : class
        {
            return m_Services.GetService(typeof(T)) as T;
        }


        #region IOutputService Members

        public void Activate()
        {
        }

        public void Clear()
        {
            OutputManager.Clear();  
        }

        public void Write(string formatString, params object[] args)
        {
            OutputManager.WriteLine(formatString, args);
        }

        public void WriteLine()
        {
            OutputManager.WriteLine();  
        }

        public void WriteLine(string formatString)
        {
            OutputManager.WriteLine(formatString);  
        }

        public void WriteLine(string formatString, params object[] args)
        {
            OutputManager.WriteLine(formatString, args);    
        }

        public void WriteException(string exc)
        {
            WriteLine(exc);
        }

        public void WriteException(Exception exc)
        {
            try
            {
                //StringBuilder sb = new StringBuilder();
                //sb.Append(DateTime.Now);
                //sb.AppendLine();
                //sb.Append(exc);
                string message = exc.ToString();
                this.WriteLine(message);

                //if (this.Log != null)
                //{
                //    if (exc is InformationalException)
                //    {
                //        this.Log.WriteEntry(exc.Message, EventLogEntryType.Warning);
                //    }
                //    else
                //    {
                //        this.Log.WriteEntry(message, EventLogEntryType.Error);
                //    }
                //}
            }
            catch //(Exception wtfExc)
            {
                // Тупо тихарим. Да.
                /*
                IAppManager app = (IAppManager)DataBinder.Services.GetService(typeof(IAppManager));
                if (app != null)
                {
                    string stackTrace = UIService.GetStackTrace(wtfExc);
                    DevExpress.XtraEditors.XtraMessageBox.Show(
                        app.Owner,
                        stackTrace,
                        app.GetAppTitle(),
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
                */
            }
        }

        //private void WriteExc(StringBuilder sb, Exception exc)
        //{
        //    if (exc == null) return;
        //    sb.AppendLine();
        //    sb.AppendFormat("{0}: {1}", exc.GetType().FullName);
        //    sb.AppendLine();
        //    sb.Append(exc.StackTrace);
        //    sb.AppendLine();
        //    //if (checkInner)
        //    //{
        //        WriteExc(sb, exc.InnerException);
        //    //}
        //}

        public void ShowWarning(string message)
        {
            MessageBox.Show(message, this.GetCaption(), MessageBoxButton.OK);
            WriteLine(message);
            //OutputManager.SetContentOutput(message);
        }

        public void ShowError(Exception exc)
        {
            MessageBox.Show(exc.ToString(), this.GetCaption(), MessageBoxButton.OK);
            WriteException(exc);
            //OutputManager.SetContentOutput(exc.ToString()); 
        }

        private string GetCaption()
        {
            string caption = Localization.App_Title;
            IAppManager app = this.GetService<IAppManager>();
            if (app != null)
            {
                caption = app.GetAppTitle();
            }

            return caption;
        }

        #endregion
    }
}
